﻿/*
	Copyright 2010 Intuitive Solutions

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

using System;
using System.IO;
using System.Xml.Xsl;
using System.Xml.XPath;

namespace Metamorph.SupportTools.Uml2Xml
{
	public class Uml2XmlConverter
	{
		public void Convert(string sPath, string sFileName, string sTargetFileName)
		{
			try
			{
				StreamReader srStreamReader = new StreamReader(sPath + Path.DirectorySeparatorChar + sFileName, System.Text.Encoding.GetEncoding("iso-8859-15"));

				string sXmiContents = srStreamReader.ReadToEnd();

				sXmiContents = sXmiContents.Replace("containedNode", "node");

				int iNamespaceIndex, iNamespaceIndexEnd;

				iNamespaceIndex		= sXmiContents.ToLower().IndexOf("xmlns:uml=\"", 0) + "xmlns:xmi=\"".Length;
				iNamespaceIndexEnd	= sXmiContents.IndexOf("\"", iNamespaceIndex);

				sXmiContents = sXmiContents.Remove(iNamespaceIndex, iNamespaceIndexEnd - iNamespaceIndex);
				sXmiContents = sXmiContents.Insert(iNamespaceIndex, "http://www.intuitivesolutions.com/uml");

				iNamespaceIndex		= sXmiContents.ToLower().IndexOf("xmlns:xmi=\"", 0) + "xmlns:xmi=\"".Length;
				iNamespaceIndexEnd	= sXmiContents.IndexOf("\"", iNamespaceIndex);

				sXmiContents = sXmiContents.Remove(iNamespaceIndex, iNamespaceIndexEnd - iNamespaceIndex);
				sXmiContents = sXmiContents.Insert(iNamespaceIndex, "http://www.intuitivesolutions.com/xmi");

				iNamespaceIndex		= sXmiContents.ToLower().IndexOf("xmlns:bpmn=\"", 0);
				iNamespaceIndexEnd	= sXmiContents.IndexOf("\"", iNamespaceIndex + "xmlns:bpmn=\"".Length);

				sXmiContents = sXmiContents.Remove(iNamespaceIndex, iNamespaceIndexEnd - iNamespaceIndex);
				sXmiContents = sXmiContents.Insert(iNamespaceIndex, "xmlns:bpmn=\"http://www.intuitivesolutions.com/bpmn");

				sXmiContents = sXmiContents.Replace("<BPMN:", "<bpmn:");

				XPathDocument xdDocument = new XPathDocument(new StringReader(sXmiContents));

				XslCompiledTransform ctTransform = new XslCompiledTransform();

				EmbeddedResourceResolver errResolver = new EmbeddedResourceResolver();
				ctTransform.Load("Templates/Uml.xslt", XsltSettings.TrustedXslt, errResolver);

				XsltArgumentList xalArgumentList = new XsltArgumentList();

				StreamWriter srStreamWriter = new StreamWriter(sPath + Path.DirectorySeparatorChar + sTargetFileName, false, System.Text.Encoding.GetEncoding("iso-8859-15"));

				ctTransform.Transform(xdDocument, xalArgumentList, srStreamWriter);

				srStreamReader.Close();
				srStreamWriter.Close();
			}
			catch (Exception eException)
			{
				System.Console.WriteLine(eException.Message);
			}
		}
	}
}
